package cn.cdzdjd.service.impl;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import cn.cdzdjd.constant.BrowserConstant;
import cn.cdzdjd.exception.RenException;
import cn.cdzdjd.service.XHSNoteVideoService;
import cn.cdzdjd.util.UrlUtil;
import cn.cdzdjd.vo.VideoInfoVO;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class XHSNoteVideoServiceImpl implements XHSNoteVideoService {

	@Override
	public VideoInfoVO getPhotoInfo(String locationUrl) {

		return null;
	}

	@Override
	public VideoInfoVO getVideoInfo(String locationUrl) {
		try {
			Document doc = Jsoup.connect(locationUrl).ignoreContentType(true).userAgent(BrowserConstant.USER_AGENT_PC)
					.header("Accept", BrowserConstant.ACCEPT).referrer(locationUrl).get();
			Element body = doc.body();
			Element script = body.lastElementChild();
			String str = script.data();
			String json = str.substring(25, str.length());

			JSONObject init = new JSONObject(json);
			JSONObject note = init.getJSONObject("note");
			String noteId = note.getString("firstNoteId");
			if (StringUtils.isBlank(noteId)) {
				throw new RenException("笔记不存在");
			}
			JSONObject childNote = note.getJSONObject("noteDetailMap").getJSONObject(noteId).getJSONObject("note");
			// 标题
			String videoTitle = childNote.getString("title");
			String desc = childNote.getString("desc");
			if (childNote.has("video")) {
				JSONObject video = childNote.getJSONObject("video");
				JSONObject media = video.getJSONObject("media");
				JSONObject stream = media.getJSONObject("stream");
				JSONObject h264_0 = stream.getJSONArray("h264").getJSONObject(0);
				JSONArray backupUrls = h264_0.getJSONArray("backupUrls");
				// 视频
				List<String> videoList = new ArrayList<>();
				for (int i = 0; i < backupUrls.length(); i++) {
					String backupUrl = backupUrls.getString(i).replace("\u002F", "/");
					videoList.add(UrlUtil.setHttps(backupUrl));
				}
				// 视频封面
				JSONObject image = childNote.getJSONArray("imageList").getJSONObject(0);
				String imageURL = image.getString("urlDefault");
				int width = image.getInt("width");
				String head = imageURL.split("!")[0];
				URL headUrl = new URL(head);
				String headPath = headUrl.getPath();
				int index = StringUtils.ordinalIndexOf(headPath, "/", 3);
				String path = StringUtils.substring(headPath, index);
				String poster = "https://ci.xiaohongshu.com" + path + "?imageView2/2/w/" + width + "/format/jpg";
				List<String> posterList = new ArrayList<>();
				posterList.add(UrlUtil.setHttps(poster));
				return new VideoInfoVO(videoList, posterList, videoTitle + "<br>" + desc, null);
			} else {
				JSONArray imageList = childNote.getJSONArray("imageList");
				List<String> posterList = new ArrayList<>();
				for (int i = 0; i < imageList.length(); i++) {
					JSONObject image = imageList.getJSONObject(i);
					String imageURL = image.getString("urlDefault");
					int width = image.getInt("width");
					String head = imageURL.split("!")[0];
					URL headUrl = new URL(head);
					String headPath = headUrl.getPath();
					int index = StringUtils.ordinalIndexOf(headPath, "/", 3);
					String path = StringUtils.substring(headPath, index);
					String poster = "https://ci.xiaohongshu.com" + path + "?imageView2/2/w/" + width + "/format/jpg";
					posterList.add(UrlUtil.setHttps(poster));
				}
				return new VideoInfoVO(null, posterList, videoTitle + "<br>" + desc, null);
			}
		} catch (IOException e) {
			e.printStackTrace();
			log.error("jsoup访问异常");
		}

		return null;
	}

}
